package zucc.edu.cn.chengchengparttime.Workdetails;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import zucc.edu.cn.chengchengparttime.MainActivity;
import zucc.edu.cn.chengchengparttime.R;
import zucc.edu.cn.chengchengparttime.Text;

public class WorkdetailsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int HEADER_VIEW=0; //头部
    private static final int NORMAL_VIEW=1; //详细信息

    private Context context;
    private List<Text> dataList; //数据列表

    public WorkdetailsAdapter(Context context, List<Text> dataList){ //构造器
        this.context=context;
        this.dataList=dataList;
    }

    //头部ViewHolder
    public class HeaderViewHolder extends RecyclerView.ViewHolder {
        public HeaderViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(v -> {
            });
        }
        //数据封装
        public TextView workname;
        public TextView workmark;
        public TextView salaryperhour;
        //getter和setter方法
        public TextView getWorkname() {
            return workname;
        }

        public void setWorkname(TextView workname) {
            this.workname = workname;
        }

        public TextView getWorkmark() {
            return workmark;
        }

        public void setWorkmark(TextView workmark) {
            this.workmark = workmark;
        }

        public TextView getSalaryperhour() {
            return salaryperhour;
        }

        public void setSalaryperhour(TextView salaryperhour) {
            this.salaryperhour = salaryperhour;
        }
    }

    //详细信息ViewHolder
    public class NormalViewHolder extends RecyclerView.ViewHolder {
        public NormalViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(v -> {
            });
        }
        //数据封装
        public TextView text;
        public TextView tip;
        public TextView worktime_txt;
        public TextView workarea_txt;
        public TextView contacts_txt;
        public TextView likenum_txt;
        public AppCompatButton btn_signup;
        //getter和setter方法
        public TextView getText() {
            return text;
        }

        public void setText(TextView text) {
            this.text = text;
        }

        public TextView getTip() {
            return tip;
        }

        public void setTip(TextView tip) {
            this.tip = tip;
        }

        public TextView getWorktime_txt() {
            return worktime_txt;
        }

        public void setWorktime_txt(TextView worktime_txt) {
            this.worktime_txt = worktime_txt;
        }

        public TextView getWorkarea_txt() {
            return workarea_txt;
        }

        public void setWorkarea_txt(TextView workarea_txt) {
            this.workarea_txt = workarea_txt;
        }

        public TextView getContacts_txt() {
            return contacts_txt;
        }

        public void setContacts_txt(TextView contacts_txt) {
            this.contacts_txt = contacts_txt;
        }

        public TextView getLikenum_txt() {
            return likenum_txt;
        }

        public void setLikenum_txt(TextView likenum_txt) {
            this.likenum_txt = likenum_txt;
        }

        public AppCompatButton getBtn_signup() {
            return btn_signup;
        }

        public void setBtn_signup(AppCompatButton btn_signup) {
            this.btn_signup = btn_signup;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType==HEADER_VIEW){ //头部
            View headerView= LayoutInflater.from(context).inflate(R.layout.activity_workdetails,null); //定义view
            HeaderViewHolder Headerholder=new HeaderViewHolder(headerView); //定义ViewHolder
            //接下来是控件的引用声明
            Headerholder.workname=(TextView) headerView.findViewById(R.id.workname);
            Headerholder.workmark=(TextView) headerView.findViewById(R.id.workmark);
            Headerholder.salaryperhour=(TextView) headerView.findViewById(R.id.salaryperhour);
            return Headerholder; //返回holder
        }else{ //详细信息
            View normalView=LayoutInflater.from(context).inflate(R.layout.activity_workdetails_item,null); //定义view
            NormalViewHolder Normalholder=new NormalViewHolder(normalView); //定义ViewHolder
            //接下来是控件的引用声明
            Normalholder.text=(TextView) normalView.findViewById(R.id.text);
            Normalholder.tip=(TextView) normalView.findViewById(R.id.tip);
            Normalholder.worktime_txt=(TextView) normalView.findViewById(R.id.worktime_txt);
            Normalholder.workarea_txt=(TextView) normalView.findViewById(R.id.workarea_txt);
            Normalholder.contacts_txt=(TextView) normalView.findViewById(R.id.contacts_txt);
            Normalholder.likenum_txt=(TextView) normalView.findViewById(R.id.likenum_txt);
            Normalholder.btn_signup=(AppCompatButton) normalView.findViewById(R.id.btn_signup);
            return Normalholder; //返回holder
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        try{
            if(holder instanceof HeaderViewHolder){ //对于头部ViewHolder
                HeaderViewHolder headerViewHolder=(HeaderViewHolder) holder;
                Text data=dataList.get(position);
                Log.d("data",data.toString());//测试

                headerViewHolder.workname.setText(data.n1);
                Log.d("headerViewHolder.workname",headerViewHolder.workname.getText().toString());//测试
                headerViewHolder.workmark.setText(data.n2);
                Log.d("headerViewHolder.workmark",headerViewHolder.workmark.getText().toString());//测试
                headerViewHolder.salaryperhour.setText(data.n3);
                Log.d("headerViewHolder.salaryperhour",headerViewHolder.salaryperhour.getText().toString());//测试
            }else if(holder instanceof NormalViewHolder){ //对于详细信息ViewHolder
                NormalViewHolder normalViewHolder=(NormalViewHolder) holder;
                Text data=dataList.get(position);
                Log.d("data",data.toString());//测试

                normalViewHolder.text.setText(data.n1);
                Log.d("normalViewHolder.text",normalViewHolder.text.getText().toString());//测试
                normalViewHolder.tip.setText(data.n2);
                Log.d("normalViewHolder.tip",normalViewHolder.tip.getText().toString());//测试
                normalViewHolder.worktime_txt.setText(data.n3);
                Log.d("normalViewHolder.worktime_txt",normalViewHolder.worktime_txt.getText().toString());//测试
                normalViewHolder.workarea_txt.setText(data.n4);
                Log.d("normalViewHolder.workarea_txt",normalViewHolder.workarea_txt.getText().toString());//测试
                normalViewHolder.contacts_txt.setText(data.n5);
                Log.d("normalViewHolder.contacts_txt",normalViewHolder.contacts_txt.getText().toString());//测试
                normalViewHolder.likenum_txt.setText(data.n6);
                Log.d("normalViewHolder.likenum_txt",normalViewHolder.likenum_txt.getText().toString());//测试
                normalViewHolder.btn_signup.setOnClickListener(view -> {
                    Toast.makeText(this.context, "点击了报名按钮" , Toast.LENGTH_SHORT).show();
                    //跳转到我的报名界面
                    Intent i=new Intent();
                    i.setClass(this.context, MainActivity.class);
                    //一定要指定是第几个pager，这里填写2
                    i.putExtra("id",2);
                    context.startActivity(i);
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //重写getItemCount方法
    @Override
    public int getItemCount() {
        return dataList.size();
    }

    //重写getItemViewType方法
    @Override
    public int getItemViewType(int position){
        if(position==0){ //第一个item就是背景
            return HEADER_VIEW;
        }else{ //其余是详细信息的各个字段
            return NORMAL_VIEW;
        }
    }
}
